package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.TableBase;
import com.yili.oa.entity.UserSearch;
import com.yili.oa.models.Admin;
import com.yili.oa.models.User;
import com.yili.oa.service.UserService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.vo.request.UserDataReq;
import com.yili.oa.vo.response.UserDataRes;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/3/20 0020
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Controller
@RequestMapping("/sys/user")
public class UserApi {

    @Autowired
    private UserService userService;

//    @Autowired
//    private RoleService roleService;




    /**
     * 获取用户列表数据
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public TableBase userList(UserSearch search) throws ParseException {
        Admin admin = (Admin) SecurityUtils.getSubject().getSession().getAttribute("admin");
        PageHelper.startPage(search.getPage(),search.getLimit());
        if (search==null) {
            search = new UserSearch();
        }
        TableBase tableBase  = new TableBase();
        List<UserDataRes> list = new ArrayList<>();
        List<User> users = userService.searchUser(search,admin);
        PageInfo pageInfo = new PageInfo<>(users,search.getLimit());
        for (User user:
                users) {

            UserDataRes udr = new UserDataRes();
            udr.setUserId(user.getUserid());
            //得到姓名需要解密
/*            Base64 base64 = new Base64();
            String name = new String(base64.decode(user.getName()));*/
            udr.setUserName(user.getName());
            //最后登录日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            if (user.getLastLoginTime()!=null) {
                String dateString = sdf.format(user.getLastLoginTime());
                Date date = sdf.parse(dateString);
                udr.setLastLoginTime(date);
            }
            //创建日期
            if (user.getCreateTime()!=null) {
                Long createT = Long.valueOf(user.getCreateTime())*1000;
                String dateString2 = sdf.format(createT);
                Date date2 = sdf.parse(dateString2);
                udr.setUserCreateTime(date2);
            }

            udr.setUserStatus(String.valueOf(user.getEnable()));
            udr.setCoin(user.getCoins().intValue());
            list.add(udr);
        }
        tableBase.setData(list);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }
    /**
     * 状态按钮编辑
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public String edit(UserDataReq userDataReq){
        if (StringUtils.isBlank(userDataReq.getUserId())){
            throw new AjaxException("用户不存在");
        }
        if (StringUtils.isBlank(userDataReq.getUserStatus())){
            throw new AjaxException("状态为空");
        }

        Boolean result = userService.updStatus(userDataReq);
        if (!result){
            return "error";
        }
        return "success";
    }

    /**
     * 重置用户密码
     * @return
     */
    @PostMapping("/reset")
    @ResponseBody
    public String userEditButton(@RequestBody Long userId){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("admin");
        if (user==null){
            return "error";
        }

        return "success";
    }
}
